var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var c = cc.gfx;
var s = function () {
  this.x = 0;
  this.y = 0;
  this.dis = 0;
  this.cos = 0;
  this.sin = 0;
};
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;
var p = _decorator.playOnFocus;
var _menu = _decorator.menu;
var _def_MotionTrail = function (e) {
  function _creator() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.atlas = null;
    t._spriteFrame = null;
    t._active = true;
    t._isWorldXY = true;
    t.offset = cc.v2(0, 0);
    t._length = 20;
    t._headWidth = 100;
    t._tailWidth = 0;
    t._headOpacity = 255;
    t._tailOpacity = 0;
    t.renderData = null;
    t.meshID = 0;
    t.capacity = 0;
    t.verticesCount = 0;
    t.indicesCount = 0;
    t.$flush = null;
    t.$xyOffset = 1e8;
    t.$uvOffset = 1e8;
    t.$colorOffset = 1e8;
    t.$step = 0;
    t.trailData = [];
    t.$getVData = function () {
      return t.renderData.vDatas[t.meshID];
    };
    t.$getUintVData = function () {
      return t.renderData.uintVDatas[t.meshID];
    };
    t.$getIData = function () {
      return t.renderData.iDatas[t.meshID];
    };
    return t;
  }
  __extends(_creator, e);
  Object.defineProperty(_creator.prototype, "$spriteFrame", {
    get: function () {
      return this._spriteFrame;
    },
    set: function (e) {
      this._spriteFrame = e;
      this.$updateSpriteFrame();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "active", {
    get: function () {
      return this._active;
    },
    set: function (e) {
      this._active = e;
      this.enabled = e;
      this.$updateActive();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "$isWorldXY", {
    get: function () {
      return this._isWorldXY;
    },
    set: function (e) {
      this._isWorldXY = e;
      this.$updateXY();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "length", {
    get: function () {
      return this._length;
    },
    set: function (e) {
      this._length = Math.max(e, 0);
      this.updateLength();
      this.updateWidth();
      this.$updateUV();
      this.$updateColor();
      this.resetPos();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "headWidth", {
    get: function () {
      return this._headWidth;
    },
    set: function (e) {
      this._headWidth = Math.max(e, 0);
      this.updateWidth();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "tailWidth", {
    get: function () {
      return this._tailWidth;
    },
    set: function (e) {
      this._tailWidth = Math.max(e, 0);
      this.updateWidth();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "headOpacity", {
    get: function () {
      return this._headOpacity;
    },
    set: function (e) {
      this._headOpacity = e;
      this.$updateColor();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "tailOpacity", {
    get: function () {
      return this._tailOpacity;
    },
    set: function (e) {
      this._tailOpacity = e;
      this.$updateColor();
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "$vDataLength", {
    get: function () {
      return this.verticesCount * this.$step;
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_creator.prototype, "$iDataLength", {
    get: function () {
      return this.indicesCount;
    },
    enumerable: false,
    configurable: true
  });
  _creator.prototype._resetAssembler = function () {
    var e = this._assembler = new g();
    e.init(this);
    e.updateRenderData = this.$onFlushed.bind(this);
    this.$flush = this.setVertsDirty;
    var t = this.renderData = new cc.RenderData();
    t.init(e);
    this.meshID = t.meshCount;
    this.$init();
  };
  _creator.prototype.$init = function () {
    this.$setVFmt();
    this.updateLength();
    this.updateWidth();
    this.node.on(cc.Node.EventType.COLOR_CHANGED, this.$updateColor, this);
    this.resetPos();
  };
  _creator.prototype.start = function () {
    this.$updateSpriteFrame();
    cc.director.once(cc.Director.EVENT_AFTER_DRAW, this.$updateColor, this);
  };
  _creator.prototype.$setVFmt = function (e) {
    undefined === e && (e = new c.VertexFormat([{
      name: c.ATTR_POSITION,
      type: c.ATTR_TYPE_FLOAT32,
      num: 2
    }, {
      name: c.ATTR_UV0,
      type: c.ATTR_TYPE_FLOAT32,
      num: 2
    }, {
      name: c.ATTR_COLOR,
      type: c.ATTR_TYPE_UINT8,
      num: 4,
      normalize: true
    }]));
    var t = this._assembler;
    cc.sys.isNative && t.setVertexFormat(e);
    var o = e._elements;
    for (var n = o.length - 1; n > -1; --n) {
      this.$step += o[n].bytes >> 2;
    }
    var i = e._attr2el;
    this.$xyOffset = i[c.ATTR_POSITION].offset >> 2;
    this.$uvOffset = i[c.ATTR_UV0].offset >> 2;
    this.$colorOffset = i[c.ATTR_COLOR].offset >> 2;
  };
  _creator.prototype.$createBuffer = function (e, t, o) {
    undefined === t && (t = e - 2);
    undefined === o && (o = 2);
    o = Math.max(o, 1.5);
    var n = this.renderData;
    this.verticesCount = Math.max(e, 0);
    this.indicesCount = Math.max(3 * t, 0);
    var i = !n.vDatas[this.meshID];
    if (this.verticesCount > this.capacity) {
      this.capacity = ~~Math.max(this.capacity * o, this.verticesCount);
      i = true;
    } else if (this.verticesCount < this.capacity / o) {
      this.capacity = ~~Math.max(this.capacity / o, this.verticesCount);
      i = true;
    }
    if (i) {
      var a = new Float32Array(this.verticesCount * this.$step);
      var r = new Uint16Array(this.indicesCount);
      n.updateMesh(this.meshID, a, r);
    }
    this.$updateIndice();
  };
  _creator.prototype.update = function () {
    cc.sys.isNative && this.$updateColor();
    this.$flush();
  };
  _creator.prototype.$onFlushed = function () {
    if (false !== this.active && null !== this.$spriteFrame && 0 !== this.length) {
      var e = this.trailData;
      for (var t = this.length - 1; t > 0; --t) {
        var o = e[t];
        var n = e[t - 1];
        o.x = n.x;
        o.y = n.y;
        o.sin = n.sin;
        o.cos = n.cos;
      }
      if (this.$isWorldXY) {
        var i = this.node._worldMatrix.m;
        this.node._updateWorldMatrix();
        e[0].x = this.offset.x + i[12];
        e[0].y = this.offset.y + i[13];
      } else {
        e[0].x = this.offset.x + this.node.x;
        e[0].y = this.offset.y + this.node.y;
      }
      this.$updateXY();
    }
  };
  _creator.prototype.$updateActive = function () {
    this.active && this.resetPos();
  };
  _creator.prototype.$updateSpriteFrame = function () {
    var e = this.$spriteFrame;
    var t = this.getMaterial(0) || cc.Material.getBuiltinMaterial("2d-sprite");
    t.define("USE_TEXTURE", true);
    t.setProperty("texture", e ? e.getTexture() : null);
    this.$updateUV();
  };
  _creator.prototype.$updateXY = function () {
    var e = this.$getVData();
    var t = null;
    var o = null;
    var n = 0;
    var i = 0;
    var a = 0;
    var r = 0;
    var c = 0;
    var s = this.$step;
    var u = 0;
    var l = 0;
    if (!this.$isWorldXY) {
      u = this.node.x;
      l = this.node.y;
    }
    var d = this.trailData;
    var p = 0;
    for (var h = this.length - 1; p < h; ++p) {
      t = d[p];
      o = d[p + 1];
      n = t.x - u;
      i = t.y - l;
      a = o.x - u;
      r = o.y - l;
      if (0 === p) {
        var f = Math.atan2(r - i, a - n);
        t.sin = Math.sin(f);
        t.cos = Math.cos(f);
      }
      e[c] = n + t.dis * t.sin;
      e[c + 1] = i - t.dis * t.cos;
      e[c += s] = n - t.dis * t.sin;
      e[c + 1] = i + t.dis * t.cos;
      c += s;
    }
    e[c] = a + o.dis * t.sin;
    e[c + 1] = r - o.dis * t.cos;
    e[c += s] = a - o.dis * t.sin;
    e[c + 1] = r + o.dis * t.cos;
  };
  _creator.prototype.$updateUV = function () {
    if (null !== this.$spriteFrame) {
      var e = this.$getVData();
      var t = this.$step;
      var o = 1 / (this.trailData.length - 1);
      var n = this.$uvOffset;
      var i = 0;
      for (var a = this.$vDataLength; n < a; n += t, ++i) {
        e[n] = 1 & i;
        e[n + 1] = 1 - o * (i >> 1);
      }
      this.$fitUV();
    }
  };
  _creator.prototype.$updateColor = function () {
    var e = this.$getUintVData();
    var t = this.length;
    var o = this.headOpacity;
    var n = (o - this.tailOpacity) / (t - 1);
    var i = this.node.opacity / 255;
    var a = this.node.color.b << 16 | this.node.color.g << 8 | this.node.color.r;
    var r = 0;
    var c = this.$colorOffset;
    for (var s = this.$step; r < t; ++r) {
      var u = (o - n * r) * i << 24 | a;
      e[c] = u;
      e[c += s] = u;
      c += s;
    }
  };
  _creator.prototype.$updateIndice = function () {
    var e = this.$getIData();
    var t = 0;
    var o = 0;
    for (var n = this.$iDataLength; t < n; ++o) {
      e[t++] = o;
      e[t++] = o + 1;
      e[t++] = o + 2;
    }
  };
  _creator.prototype.updateLength = function () {
    var e = this.length;
    this.trailData = [];
    for (var t = 0; t < e; ++t) {
      this.trailData[t] = new s();
    }
    this.$createBuffer(e << 1);
  };
  _creator.prototype.updateWidth = function () {
    var e = this.trailData;
    var t = this.length;
    var o = .5 * this.headWidth;
    var n = (o - .5 * this.tailWidth) / (t - 1);
    for (var i = 0; i < t; ++i) {
      e[i].dis = o - n * i;
    }
  };
  _creator.prototype.resetPos = function () {
    var e = this.trailData;
    var t = this.offset.x;
    var o = this.offset.y;
    if (this.$isWorldXY) {
      var n = this.node._worldMatrix.m;
      this.node._updateWorldMatrix();
      t += n[12];
      o += n[13];
    } else {
      t += this.node.x;
      o += this.node.y;
    }
    for (var i = this.length - 1; i > -1; --i) {
      e[i].x = t;
      e[i].y = o;
    }
    var a = this.$getVData();
    var r = this.$step;
    i = 0;
    for (var c = this.$vDataLength; i < c; i += r) {
      a[i] = t;
      a[i + 1] = o;
    }
  };
  _creator.prototype.$fitUV = function () {
    if (null !== this.$spriteFrame) {
      var e = this.$step;
      var t = this.$spriteFrame.getTexture().width;
      var o = this.$spriteFrame.getTexture().height;
      var n = this.$spriteFrame.getRect();
      var i = this.$getVData();
      if (this.$spriteFrame._rotated) {
        var a = this.$uvOffset;
        var r = 0;
        for (var c = this.$vDataLength; a < c; a += e, ++r) {
          var s = i[a];
          i[a] = ((1 - i[a + 1]) * n.height + n.x) / t;
          i[a + 1] = (s * n.width + n.y) / o;
        }
      } else {
        a = this.$uvOffset;
        r = 0;
        for (c = this.$vDataLength; a < c; a += e, ++r) {
          i[a] = (i[a] * n.width + n.x) / t;
          i[a + 1] = (i[a + 1] * n.height + n.y) / o;
        }
      }
    }
  };
  _creator.prototype.onDestroy = function () {
    this.node.targetOff(this);
  };
  __decorate([_property({
    type: cc.SpriteAtlas,
    editorOnly: true,
    readonly: true,
    // displayName: false
  })], _creator.prototype, "atlas", undefined);
  __decorate([_property], _creator.prototype, "_spriteFrame", undefined);
  __decorate([_property({
    type: cc.SpriteFrame,
    // displayName: false
  })], _creator.prototype, "$spriteFrame", null);
  __decorate([_property], _creator.prototype, "_active", undefined);
  __decorate([_property({
    // displayName: false,
    // tooltip: false
  })], _creator.prototype, "active", null);
  __decorate([_property], _creator.prototype, "_isWorldXY", undefined);
  __decorate([_property({
    // displayName: false,
    // tooltip: false
  })], _creator.prototype, "$isWorldXY", null);
  __decorate([_property({
    // displayName: false
  })], _creator.prototype, "offset", undefined);
  __decorate([_property], _creator.prototype, "_length", undefined);
  __decorate([_property({
    type: cc.Integer,
    // displayName: false
  })], _creator.prototype, "length", null);
  __decorate([_property], _creator.prototype, "_headWidth", undefined);
  __decorate([_property({
    // displayName: false
  })], _creator.prototype, "headWidth", null);
  __decorate([_property], _creator.prototype, "_tailWidth", undefined);
  __decorate([_property({
    // displayName: false
  })], _creator.prototype, "tailWidth", null);
  __decorate([_property], _creator.prototype, "_headOpacity", undefined);
  __decorate([_property({
    type: cc.Integer,
    min: 0,
    max: 255,
    slide: true,
    // displayName: false
  })], _creator.prototype, "headOpacity", null);
  __decorate([_property], _creator.prototype, "_tailOpacity", undefined);
  __decorate([_property({
    type: cc.Integer,
    min: 0,
    max: 255,
    slide: true,
    // displayName: false
  })], _creator.prototype, "tailOpacity", null);
  return __decorate([_ccclass, p, _menu("Comp/MotionTrail")], _creator);
}(cc.RenderComponent);
exports.default = _def_MotionTrail;
var g = function (e) {
  function t() {
    return null !== e && e.apply(this, arguments) || this;
  }
  __extends(t, e);
  t.prototype.fillBuffers = function (e) {
    var t = e.renderData.vDatas[e.meshID];
    var o = e.renderData.iDatas[e.meshID];
    var n = cc.renderer._handle._meshBuffer;
    var i = n.request(e.verticesCount, e.indicesCount);
    var a = i.byteOffset >> 2;
    var r = n._vData;
    if (t.length + a > r.length) {
      r.set(t.subarray(0, r.length - a), a);
    } else {
      r.set(t, a);
    }
    var c = n._iData;
    var s = i.indiceOffset;
    var u = i.vertexOffset;
    var l = 0;
    for (var d = o.length; l < d; l++) {
      c[s++] = u + o[l];
    }
  };
  return t;
}(cc.Assembler);